#ifndef MQ_DEMO_H
#define MQ_DEMO_H

#include <iostream>
#include <string>
#include <thread>
#include <atomic>
#include <chrono>
#include <random>
#include <json.hpp>
#include <opencv2/opencv.hpp>
#include "CProducer.h"  // RocketMQ头文件
#include "CMessage.h"
#include "CSendResult.h"

using json = nlohmann::json;

class MQDemo {
public:
    MQDemo(const std::string& name_server, const std::string& topic, 
           int send_interval_ms = 1000);
    ~MQDemo();

    void start();
    void stop();
    bool isRunning() const { return running_; }

private:
    void sendLoop();
    bool initRocketMQ();
    void cleanupRocketMQ();
    bool sendRocketMQMessage(const json& msg);
    
    json buildMQMessage(const std::string& class_name,
                       const std::string& stream_id,
                       const std::string& small_uuid,
                       const std::string& small_img_path,
                       const std::string& large_img_path,
                       const cv::Rect& crop_box,
                       const cv::Size& orig_size,
                       time_t timestamp);
    
    std::string replaceBasePath(const std::string& original_path,
                               const std::string& base_path,
                               const std::string& base_url,
                               const std::string& remove_path);
    
    bool sendMessage(const json& msg);  // 修改返回类型为bool
    void generateRandomData(json& msg);

    std::string name_server_;
    std::string topic_;
    int send_interval_ms_;
    std::atomic<bool> running_;
    std::thread send_thread_;
    
    // RocketMQ相关
    CProducer* producer_;
    bool mq_initialized_;
    
    // 模拟配置
    struct Config {
        struct Path {
            std::string imgBasePath = "/home/data/images";
            std::string bigImgPath = "/home/data/big_images";
            std::string baseurl = "http://192.168.1.100:8080";
            std::string removePath = "/home/data";
        } path;
    } config_;
    
    // 随机数生成器
    std::random_device rd_;
    std::mt19937 gen_;
    std::uniform_int_distribution<> class_dist_;
    std::uniform_int_distribution<> coord_dist_;
    std::uniform_int_distribution<> size_dist_;
};

#endif // MQ_DEMO_H